02. Intro to Fragments

Intro to Fragments

There are many possible Android activities that could benefit from embedding a collection of preference editing widgets, so the Android framework team wanted to make this task as easy as possible.

You could imagine a world where this is done with a hypothetical “PreferencesView,” which you could drop into your activity's layout. However, we need a way to make the “PreferencesView” a little smarter than a simple View, but we don't want to make it a full-blown activity. It only needs to be a little piece of an activity, like, a fragment of an activity.

A Fragment behaves a lot like a mini-activity that lives inside a proper activity. Fragments were introduced as a way to make more flexible layouts for tablets and other Android devices with large screens.

Fragments can be reused inside multiple activities, and a single activity can make use of multiple fragments. A very common pattern is for a tablet UI to use two fragments side by side.

You don't need to know much more about Fragments for now, and we're not going do anything complex with multiple fragments in this course. All we're going to do is use a PreferenceFragment inside our SettingsActivity to present a list of preferences that the user can edit.

For more advanced reading on fragments, check out Creating Single-pane and Multi-pane Layouts and Building a Flexible UI.